<?php

namespace app\storeapi\controller;
use app\store\model\Wxapp as WxappModel;
use app\storeapi\model\Gifts as GiftsModel;


/**
 * 爆品礼管理控制器
 * Class Gifts
 * @package app\store\controller
 */
class Gifts extends Controller
{

    /**
     * 获取当前商户的分销方案
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function dealerCase()
    {
        //获取该商户的分销方案
        $dealerCase = WxappModel::dealerCase($this->wxapp_id);

        return $this->renderSuccess($dealerCase);

    }


    /**
     * 爆品礼列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function giftsList()
    {
        // 获取全部商品列表
        $model = new GiftsModel;
        $list = $model->giftsList($this->postData());

        return $this->renderSuccess($list);

    }



    /**
     * 商品详情
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function giftsDetail()
    {
        $param = $this->postData();
        // 商品详情
        $model = GiftsModel::detail($param['gifts_id']);
        if(empty($model)){
            return $this->renderError('爆品礼不存在，请刷新后重试');
        }

        $model['gifts_content'] = htmlspecialchars_decode($model['gifts_content']);
        //获取该商户的分销方案
        $dealerCase = WxappModel::dealerCase($this->wxapp_id);

        return $this->renderSuccess(compact('model','dealerCase'));
    }

    /**
     * 删除商品
     * @param $goods_id
     * @param boolean $goodsDel
     * @return array
     */
    public function giftsDel()
    {
        $param = $this->postData();
        // 商品详情
        $model = GiftsModel::detail($param['gifts_id']);
        if(empty($model)){
            return $this->renderError('爆品礼不存在，请刷新后重试');
        }
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }

        return $this->renderSuccess([],'操作成功');
    }

    /**
     * 修改商品状态
     * @param $gifts_id
     * @param boolean $state
     * @return array
     */
    public function giftsState()
    {
        $param = $this->postData();
        // 商品详情
        $model = GiftsModel::detail($param['gifts_id']);
        if(empty($model)){
            return $this->renderError('爆品礼不存在，请刷新后重试');
        }
        if (!$model->setStatus($param['gifts_status'])) {
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess([],'操作成功');
    }



    /**
     * 商品添加
     * @return array|mixed
     * @throws \think\exception\PDOException
     */
    public function giftsAdd()
    {
        //获取数据
        $params = $this->postData();
        $model = new GiftsModel;
        if (!$model->add($params)) {
            return $this->renderError($model->getError() ?: '添加失败');
        }

        return $this->renderSuccess([],'添加成功');

    }


    /**
     * 商品编辑
     * @param $goods_id
     * @return array|bool|mixed
     */
    public function giftsEdit()
    {
        //获取数据
        $params = $this->postData();
        $model = GiftsModel::detail($params['gifts_id']);
        
        if (!$model->edit($params)) {
            return $this->renderError($model->getError() ?: '编辑失败');
        }
        return $this->renderSuccess([],'编辑成功');
    }



}



